package com.koudai.reader.conponment;

import java.io.Serializable;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.koudai.reader.ReadBookActivity;
import com.koudai.reader.android.R;
import com.koudai.reader.util.Utils;
import com.koudai.reader.vo.Book;

public class BookChaptersDialog extends Dialog implements OnScrollListener{

    private ListView list;
    private TextView title;
    private ChapterListAdatper adapter;
    private Context context;
    private Book book;
    
    public BookChaptersDialog(Context context, Book book) {
        super(context);
        this.context = context;
        this.book = book;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        book.setChapterTitleList(Utils.getChapterList(book));
        
        setContentView(R.layout.book_chapters);

        list = (ListView) findViewById(R.id.chapters);
        title = (TextView) findViewById(R.id.book_chapter_title);

        title.setText(book.getTitle());

        adapter = new ChapterListAdatper();
        
        
        list.setAdapter(adapter);
        
        list.setSelection(book.getCurrentChapter());
        adapter.notifyDataSetChanged();
    }
    
    class ChapterListAdatper extends BaseAdapter {

        public int count;
        LayoutInflater inflater;
        @Override
        public int getCount() {
            adapter.count = book.getChapterNumber();
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return count;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            String title = book.getChapterTitleList().get(position) == null ? "" : book.getChapterTitleList().get(position);
            
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.book_chapter_item, null);
                
                TextView textview = (TextView) convertView.findViewById(R.id.chapter_summary);
                                
                if (position == book.getCurrentChapter()) {
                    textview.setText(Html.fromHtml("<font color=red>" + (title.length() >= 30 ? title.substring(0, 30) : title) + "</font>"));
                } else {
                    textview.setText(title.length() >= 30 ? title.substring(0, 30) : title);
                }
                
                ListViewItemHolder viewHolder = new ListViewItemHolder();
                viewHolder.textview = textview;
                convertView.setTag(viewHolder);

                
                
     
            } else {
                
                ListViewItemHolder holder = (ListViewItemHolder)convertView.getTag();
                if (position == book.getCurrentChapter()) {
                    holder.textview.setText(Html.fromHtml("<font color=red>" + (title.length() >= 30 ? title.substring(0, 30) : title) + "</font>"));
                } else {
                    holder.textview.setText(title.length() >= 30 ? title.substring(0, 30) : title);
                }
            }
      
            convertView.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    book.setCurrentChapter(position);
                    book.setCurrentposition(0);
                    Intent intent = new Intent();
                    intent.setClass(context, ReadBookActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("book", (Serializable) book);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                    
                    dismiss();
                }
            }); 
            return convertView;
        }

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
        
    }
    
    class ListViewItemHolder { 
        TextView textview;
    }

}
